<template>
  <div class="app-container">
    <el-card style="margin-top: -3px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="竞赛类别" prop="type">
          <el-select v-model="queryParams.type" placeholder="全部" clearable style="width: 114px">
            <el-option
              v-for="dict in dict.type.contest_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="竞赛等级" prop="grade" style="margin-left:0px">
          <el-select v-model="queryParams.grade" placeholder="全部" clearable style="width: 85px">
            <el-option
              v-for="dict in dict.type.contest_grade"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="竞赛频次" prop="freq" style="margin-left: 0px">
          <el-select v-model="queryParams.freq" placeholder="全部" clearable style="width: 114px">
            <el-option
              v-for="dict in dict.type.contest_freq"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="省赛/区域赛" prop="need_fee1" style="margin-left: 0px">
          <el-select v-model="queryParams.need_fee1" placeholder="全部" clearable style="width: 85px">
            <el-option
              v-for="dict in dict.type.contest_fee_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>


        <el-form-item label="国赛/决赛" prop="need_fee2" style="margin-left: 0px">
          <el-select v-model="queryParams.need_fee2" placeholder="全部" clearable style="width: 85px">
            <el-option
              v-for="dict in dict.type.contest_fee_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="delFlag" style="margin-left: 0px">
          <el-select v-model="queryParams.delFlag" placeholder="全部" clearable style="width: 85px">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"
                     size="mini" @click="handleQuery" style="height: 30px">搜索
          </el-button>
          <el-button icon="el-icon-refresh"
                     size="mini" @click="resetQuery" style="height: 30px">重置
          </el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['contest:contestInfo:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['contest:contestInfo:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['contest:contestInfo:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['contest:contestInfo:export']"
          >导出
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-popover
            placement="bottom"
            width="190"
            trigger="hover"
            content="生成本年度子竞赛信息">
            <el-button
              type="warning"
              plain
              slot="reference"
              icon="el-icon-magic-stick"
              size="mini"
              @click="makeInsert"
              v-hasPermi="['contest:contestInfo:insert']"
            >
              生成
            </el-button>
          </el-popover>

        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" @cell-click="showSubContest" :data="contestInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center"/>
        <el-table-column label="竞赛名称" align="center" prop="name">
<!--          <template slot-scope="scope">-->
<!--            <a @click="showSubContest(scope.row.contestId)"> {{ scope.row.name }}</a>-->
<!--          </template>-->
        </el-table-column>
        <el-table-column label="竞赛官网" align="center" prop="url" width="180%"/>
        <el-table-column label="入选年份" align="center" prop="in_year" width="75px"/>
        <el-table-column label="落选年份" align="center" prop="out_year" width="75px"/>
        <el-table-column label="教育部认可" align="center" prop="inMinistry" width="90px">
          <template slot-scope="scope">
            <a v-if="scope.row.inMinistry == 1 || scope.row.inMinistry == 'Y' || scope.row.inMinistry == 'y'">是</a>
            <a v-else-if="scope.row.inMinistry != 1|| scope.row.inMinistry == 'N' || scope.row.inMinistry == 'n'">否</a>
          </template>
        </el-table-column>
        <el-table-column label="赛事编号" align="center" prop="ministryNo" width="100%"/>
        <el-table-column label="竞赛类别" align="center" prop="type" width="90%">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.contest_type" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <el-table-column label="竞赛等级" align="center" prop="grade" width="80%">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.contest_grade" :value="scope.row.grade"/>
          </template>
        </el-table-column>
        <el-table-column label="竞赛频次" align="center" prop="freq" width="90%">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.contest_freq" :value="scope.row.freq"/>
          </template>
        </el-table-column>
        <el-table-column label="省赛/区域赛" align="center" prop="need_fee1" width="90%">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.contest_fee_status" :value="scope.row.need_fee1"/>
          </template>
        </el-table-column>
        <el-table-column label="国赛/决赛" align="center" prop="need_fee2" width="80%">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.contest_fee_status" :value="scope.row.need_fee2"/>
          </template>
        </el-table-column>
        <el-table-column label="启用状态" align="center" width="72">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.delFlag"
              active-color="#ff4949"
              inactive-color="#13ce66"
              active-value="1"
              inactive-value="0"
              @change="changeState(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="70px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['contest:contestInfo:edit']"
              style="margin-right: 3px;margin-left: 1px;font-size: 16px"
            ></el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['contest:contestInfo:remove']"
              style="margin-left: 3px;font-size: 16px"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <el-card v-show="showSubC" style="margin-top: 5px">
      <indexmini></indexmini>
    </el-card>

    <!-- 添加或修改竞赛基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px" style="margin-top: -15px">
        <el-row>

          <el-col>
            <el-form-item label="竞赛名称" prop="name" style="margin-bottom: 7px">
              <el-input v-model="form.name" placeholder="请输入竞赛名称" :rows="1"/>
            </el-form-item>
          </el-col>

          <el-col>
            <el-form-item label="竞赛官网" prop="url" style="margin-bottom: 7px">
              <el-input v-model="form.url" placeholder="请输入竞赛网址" :rows="1"/>
            </el-form-item>
          </el-col>

          <el-col>
            <el-form-item label="竞赛简介" style="margin-bottom: 7px">
              <editor v-model="form.brief" :min-height="180"/>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="教育部认可" style="margin-bottom: 7px">
              <el-select v-model="form.inMinistry" style="width: 118px">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="18">
            <el-form-item label="赛事序号" style="margin-bottom: 7px;margin-left: 79px">
              <el-input v-model="form.ministryNo" placeholder="请输入赛事序号" :rows="1" style="width: 249px"/>
            </el-form-item>
          </el-col>

          <el-col :span="7">
            <el-form-item label="竞赛类别" prop="type" style="margin-bottom: 7px">
              <el-select v-model="form.type" placeholder="全部" style="width: 119px">
                <el-option
                  v-for="dict in dict.type.contest_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="7">
            <el-form-item label="竞赛等级" prop="grade" style="margin-bottom: 7px;margin-left: 24px">
              <el-select v-model="form.grade" placeholder="全部" style="width: 80px">
                <el-option
                  v-for="dict in dict.type.contest_grade"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="7">
            <el-form-item label="竞赛频次" prop="freq" style="margin-bottom: 7px;margin-left: 9px">
              <el-select v-model="form.freq" placeholder="全部" style="width: 116px">
                <el-option
                  v-for="dict in dict.type.contest_freq"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="省赛/区域赛" prop="need_fee1"
                          style="margin-bottom: 7px">
              <el-switch
                style="display: block;margin-top: 7px"
                v-model="form.need_fee1"
                active-color="#ff4949"
                inactive-color="#13ce66"
                :active-value="1"
                :inactive-value="0"
                active-text="付费"
                inactive-text="免费">
              </el-switch>

            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="国赛/决赛" prop="need_fee2"
                          style="margin-bottom: 7px">
              <el-switch
                style="display: block;margin-top: 7px"
                v-model="form.need_fee2"
                active-color="#ff4949"
                inactive-color="#13ce66"
                :active-value="1"
                :inactive-value="0"
                active-text="付费"
                inactive-text="免费">
              </el-switch>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item label="入选年份" prop="type" style="margin-bottom: 7px">
              <el-date-picker
                v-model="form.in_year"
                format="yyyy"
                value-format="yyyy"
                style="width: 178px"
                :picker-options="pickerOptions"
                size="large"
                type="year"
                placeholder="选择年份">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="落选年份" prop="type" style="margin-bottom: 7px;margin-left: 200px">
              <el-date-picker
                v-model="form.out_year"
                :picker-options="pickerOptions"
                format="yyyy"
                value-format="yyyy"
                style="width: 178px"
                size="large"
                type="year"
                placeholder="选择年份">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col>
            <el-form-item label="备注" prop="remark" style="margin-bottom: 7px">
              <el-input v-model="form.remark" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listContestInfo,
  getContestInfo,
  delContestInfo,
  addContestInfo,
  updateContestInfo,
  pseudoDeletion,
  switchState
} from "@/api/contest/contestInfo";
import {makeInsert} from "@/api/contest/subContest";
import indexmini from "@/views/contest/subContest/indexmini";
import item from "@/layout/components/Sidebar/Item.vue";
import log from "@/views/monitor/job/log.vue";
import year from "@/components/Crontab/year.vue";

export default {
  name: "ContestInfo",
  //刷新
  inject: ['reload'],
  dicts: ['contest_type', 'sys_normal_disable', 'contest_grade', 'contest_freq', 'contest_fee_status', 'sys_yes_no'],
  data() {
    return {
      //设置选择日期
      pickerOptions: {
        //disabled为函数，返回值为布尔值，
        disabledDate: (time) => {
          let minYear = new Date().getFullYear() - 30
          return time > Date.now() || time.getTime() < new Date(JSON.stringify(minYear))
        }
      },
      options: [
        {
          values: "1",
          label: "是"
        },
        {
          values: "0",
          label: "否"
        }
      ],
      values: '1',
      //是否展示子竞赛
      showSubC: false,
      contestValue: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的竞赛数据数组
      contestNameList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 竞赛基础信息表格数据
      contestInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        inMinistry: null,
        type: null,
        grade: null,
        freq: null,
        need_fee1: null,
        need_fee2: null,
        delFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询竞赛基础信息列表 */
    getList() {
      this.loading = true;
      listContestInfo(this.queryParams).then(response => {
        this.contestInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contestId: null,
        name: null,
        url: null,
        brief: null,
        inMinistry: 'Y',
        ministryNo: null,
        type: null,
        grade: null,
        freq: null,
        need_fee1: null,
        need_fee2: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contestId)
      this.contestNameList = selection.map(item => item.name)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加竞赛基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.reset();
      const contestId = row.contestId || this.ids
      getContestInfo(contestId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改竞赛基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.contestId != null) {
            updateContestInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContestInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contestIds = row.contestId || this.ids;
      const contestNames = row.name || this.contestNameList;
      this.$modal.confirm('是否确认删除竞赛竞赛名称为 " ' + contestNames + ' " 的竞赛信息？此操作将永久删除数据库内容，请咨询技术人员后操作！').then(function () {
        return delContestInfo(contestIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('contest/contestInfo/export', {
        ...this.queryParams
      }, `contestInfo_${new Date().getTime()}.xlsx`)
    },
    /** 修改状态*/
    changeState(row) {
      const contestIds = row.contestId;
      const contestName = row.name;
      var flags;
      row.delFlag === "0" ? flags = '启用' : flags = '禁用';
      let switchInfo = {
        delFlag: row.delFlag,
        contestId: contestIds
      }
      this.$modal.confirm('是否确认将 " ' + contestName + ' " 竞赛' + flags + '？').then(function () {
        return switchState(switchInfo)
      }).then(()=>{
        this.getList()
        this.$modal.msgSuccess("修改成功")
      }).catch(() => {
        this.getList()
      });
    },
    // 展示子竞赛
    showSubContest(row) {
      this.contestValue = row.contestId;
      //消息提示
      if (this.showSubC === false) {
        this.$message({
          message: "请下滑查看子竞赛内容！",
          type: "success",
          center: true
        })
      } else {
        this.$message({
          message: "已关闭子竞赛！",
          type: "warning",
          center: true
        })
      }

      /*传值操作*/
      this.$bus.emit("pass item", this.contestValue)

      // 是否展示
      this.showSubC = !this.showSubC
    },
    //生成当前年份子竞赛数据
    makeInsert(){
      let nowDate = new Date();
      let nowYear = nowDate.getFullYear();
      this.$modal.confirm('是否确认生成" ' + nowYear + ' " 的子竞赛信息？').then(function () {
        return makeInsert();
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("生成成功，点击查看。请手动修改比赛日期等其他信息！");
      }).catch(() => {
      });
    },
  },
  components: {
    indexmini,
  },
};
</script>
<style>
.dialogDiv {
  height: 500px;
  overflow: auto;
}

.dialog-footer {
  margin-top: -20px;
}
</style>
